package com.agu.config.ds;



import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;

@Configuration
public class DataSourceConfig {

	@Primary
	@Bean(name = "primaryDataSource")
	@Qualifier("primaryDataSource")
	@ConfigurationProperties(prefix = "spring.datasource.primary")
	public DataSource setDataSource() {
		DruidDataSource datasource  = DruidDataSourceBuilder.create().build();
        StatFilter statFilter = new StatFilter();
        statFilter.setLogSlowSql(true);
        statFilter.setMergeSql(true);
        statFilter.setSlowSqlMillis(1000);
        
        WallFilter wallFilter = new WallFilter();
        //允许执行多条SQL
        WallConfig config = new WallConfig();
        config.setMultiStatementAllow(true);
        wallFilter.setConfig(config);
		
        List<Filter> filters = new ArrayList<>();
        filters.add(statFilter);
        filters.add(wallFilter);
        datasource.setProxyFilters(filters);
		
		return datasource;
		
	}

	@Bean(name = "secondDataSource")
	@Qualifier("secondDataSource")
	@ConfigurationProperties(prefix = "spring.datasource.second") // application.properteis中对应属性的前缀
	public DataSource secondDataSource() {
		//return DruidDataSourceBuilder.create().build();
		
		DruidDataSource datasource  = DruidDataSourceBuilder.create().build();
		
        StatFilter statFilter = new StatFilter();
        statFilter.setLogSlowSql(true);
        statFilter.setMergeSql(true);
        statFilter.setSlowSqlMillis(1000);
        
        WallFilter wallFilter = new WallFilter();
        //允许执行多条SQL
        WallConfig config = new WallConfig();
        config.setMultiStatementAllow(true);
        wallFilter.setConfig(config);
		
        List<Filter> filters = new ArrayList<>();
        filters.add(statFilter);
        filters.add(wallFilter);
        datasource.setProxyFilters(filters);
		
		return datasource;
	}
	
	
	@Bean(name = "primaryJdbcOperations")
	public JdbcOperations primaryJdbcOperations(@Qualifier("primaryDataSource") DataSource primaryDataSource) {
	    return new JdbcTemplate(primaryDataSource);
	}
	 
	@Bean(name = "secondJdbcOperations")
	public JdbcOperations secondJdbcOperations(@Qualifier("secondDataSource") DataSource secondDataSource) {
	    return new JdbcTemplate(secondDataSource);
	}
	
	@Primary
	@Bean(name = "primaryTransactionManager")
	public PlatformTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource primaryDataSource) {
	    return new DataSourceTransactionManager(primaryDataSource);
	}
	 
	@Bean(name = "secondTransactionManager")
	public PlatformTransactionManager secondTransactionManager(@Qualifier("secondDataSource") DataSource secondDataSource) {
	    return new DataSourceTransactionManager(secondDataSource);
	}

}